package com.bawei.shoppingcar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

import com.bawei.shoppingcar.adapter.CarAdapter;
import com.bawei.shoppingcar.entity.CarEntity;
import com.bawei.shoppingcar.utils.HttpUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class CarActivity extends AppCompatActivity {

    private ListView lv;
    private CarAdapter adpter;
    private List<CarEntity.DataBean> carList = new ArrayList<>();
    private CheckBox cbAll;
    private TextView tvMoney;
    private Button btAccount,btDelete;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car);
        tvMoney = findViewById(R.id.tv_money);
        tvMoney.requestFocus();
        cbAll = findViewById(R.id.cb_all);
        btAccount = findViewById(R.id.bt_account);
        btDelete = findViewById(R.id.bt_delete);
        lv = findViewById(R.id.lv);
        adpter = new CarAdapter(carList,CarActivity.this);
        lv.setAdapter(adpter);
        new CarTask().execute("http://43.143.146.165:8083/car/getCars?username=zhoukun");
        //全选和取消全选
        cbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                for (CarEntity.DataBean dataBean : carList) {
                    dataBean.setCheck(b);
                }
                adpter.notifyDataSetChanged();
                total();//计算总价
            }
        });
        //删除选中的:倒序遍历
        btDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = carList.size() - 1; i >= 0; i--) {
                    if (carList.get(i).isCheck()){
                        carList.remove(i);
                    }
                }
                adpter.notifyDataSetChanged();
                total();//计算总价
            }
        });
    }
    class CarTask extends AsyncTask<String,Void,String> {
        @Override
        protected String doInBackground(String... strings) {
            return HttpUtils.doGet(strings[0]);
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            CarEntity carEntity = new Gson().fromJson(s, CarEntity.class);
            List<CarEntity.DataBean> data = carEntity.getData();
            carList.addAll(data);
            adpter.notifyDataSetChanged();
        }
    }

    //计算总价
    public void total(){
        int num = 0;
        double money = 0;
        for (CarEntity.DataBean dataBean : carList) {
            if(dataBean.isCheck()){
                num+=dataBean.getCar_num();
                money+=dataBean.getCar_num()*dataBean.getGoods_price();
            }
        }
        tvMoney.setText("总价：¥ "+money);
        btAccount.setText("结算（"+num+"）");
    }
}